import config from '../config'
import store from '../store'

//判读是否空对象或空数组
export const isEmptyObj=(data)=>{
	return (
	    (Array.isArray(data) && data.length == 0) || (Object.prototype.isPrototypeOf(data) && Object.keys(data).length == 0)
	);
}




// 是否为空
export const isEmpty = (data) => {
	return data === '' || data === null || data === undefined || (typeof data === 'number' && isNaN(data))
}



export const checkPhone=(tel)=>{
	 if(/^1[3456789]\d{9}$/.test(tel)){
		 return 1
	}
	return 0
}

export const checkPwd=(tel)=>{
	 if(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/.test(tel)){
		 return 1
	}
	return 0
}


// 获取链接参数
export const getParams = (url = window.location.href) => {
	const arr = url.split('?')
	const query = arr[1]
	const paramsArr = query.split('&')
	const params = {}
	paramsArr.map((item) => {
		const arrs = item.split('=')
		params[arrs[0]] = arrs[1]
	})
	return params
}



// 随机数
export const random = (n, m) => {
	return Math.floor(Math.random() * (m - n + 1) + n)
}

// 参数对象转参数字符串
export const getParamStr = (params = {}) => {
	const query = []
	let str = ''
	for (let key in params) {
		const data = params[key]
		if (!isEmpty(data)) {
			query.push(`${key}=${params[key]}`)
		}
	}
	str = query.join('&')
	if (query.length) {
		str = '?' + str
	}
	return str
}

// 链接跳转
export const navigateTo = (url = '', params = {}, redirect = false) => {
	const urls = url.split('/').filter(item => !!item)
	const routes = ['pages', ...urls]
	if (urls.length === 1) {
		routes.push(urls[urls.length - 1])
	}
	const paramsStr = getParamStr(params)
	let router = '/' + routes.join('/')
	// 需要登录的页面先去登录
	if (!store.state.user.token) {
		return uni.navigateTo({
			url: router
		})
	}
	router += paramsStr
	const modal = redirect ? uni.redirectTo : uni.navigateTo
	return modal({
		url: router
	})
}




// 打电话
export const callPhone = (phoneNumber) => {
	phoneNumber = String(phoneNumber)
	return uni.makePhoneCall({
		phoneNumber
	})
}

// 图片预览
export const previewImage = (params = {}) => {
	if (getType(params) === 'string') {
		params = {
			urls: [params]
		}
	} else if (getType(params) === 'array') {
		params = {
			urls: params
		}
	}
	params.urls.map((url, key) => {
		if (url.indexOf('://') === -1) {
			params.urls[key] = config.imageUrl + url
		}
	})
	const current = params.urls[0]
	return uni.previewImage({
		loop: false,
		indicator: 'number',
		current,
		...params
	})
}

//获取缓存使用同步
export const getCache = (key)=>{
	try{
		var value=JSON.parse(uni.getStorageSync(key) || '{expire:0}');
		if(value.expire==0) return {};
		if(expire>0){
			if(value.expire < new Date().getTime()){ //过期
				uni.removeStorageSync(key);
				return {};
			}
		}
		return value.data;
	}catch(e){
		return {};
	}
}

//设置缓存 默认过期时间3600秒
export const setCache = (key,data,expire=3600)=>{
	if(!key || !data) return 0;
		var value = {'expire':(new　Date()).getTime()+expire,'data':data};
		try{
			uni.setStorageSync(key,JSON.stringify(value));
			return 1;
		}catch(e){
			return 0;
		}
}